package com.mes.cloud.web;


import com.mes.cloud.api.dto.request.area.*;
import com.mes.cloud.api.dto.request.organization.BaseOrganizationAreaPageRequest;
import com.mes.cloud.api.dto.response.area.*;
import com.mes.cloud.api.dto.response.organization.SysOrganizationDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.BaseAreaService;
import com.mes.cloud.service.SysOrganizationAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Mr.zhang
 * @version 2019-07-29 18:14:59
 * @email 407892370@qq.com
 */
@Api(tags = "行政区划表", value = "行政区划表接口")
@RestController
@RequestMapping(value = "/baseArea")
public class BaseAreaController  {

    private BaseAreaService baseAreaService;
    private SysOrganizationAreaService sysOrganizationAreaService;
    @Autowired
    public BaseAreaController(BaseAreaService baseAreaService,
                              SysOrganizationAreaService sysOrganizationAreaService) {
        this.baseAreaService = baseAreaService;
        this.sysOrganizationAreaService = sysOrganizationAreaService;
    }

    /**
     * @description 根据父ID查询行政区划列表
     * @author dongyi
     * @date 2019-08-03 09:49
     **/
    @ApiOperation(value = "根据父ID查询行政区划列表", notes = "根据父ID查询行政区划列表")
    @ApiImplicitParam(paramType = "String", name = "parentId", value = "父ID", required = true)
    @GetMapping(value = "/areaList/{parentId}")
    public JsonResult<List<BaseAreaDto>> areaList(@NotEmpty(message = "父ID不能为空") @PathVariable String parentId) {
        List<BaseAreaDto> cityList = baseAreaService.selectAreaList(parentId);
        JsonResult<List<BaseAreaDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpStatusEnum.OK.getCode()).setData(cityList);
    }

    /**
     * @description 行政区划表新增
     * @author dongyi
     * @date 2019-08-03 09:49
     **/
    @ApiOperation(value = "行政区划表新增", notes = "行政区划表新增")
    @PostMapping(value = "/add")
    public JsonResult add(@Validated @RequestBody BaseAreaRequest baseAreaRequest) {
        return baseAreaService.insertSelective(baseAreaRequest);
    }

    /**
     * @description 行政区划表修改
     * @author dongyi
     * @date 2019-08-03 09:49
     **/
    @ApiOperation(value = "行政区划表修改", notes = "行政区划表修改")
    @PostMapping(value = "/update")
    public JsonResult update(@Validated @RequestBody BaseAreaRequest baseAreaRequest) {
        return baseAreaService.updateSelectiveById(baseAreaRequest);
    }

    /**
     * @description 禁用启用行政区划
     * @author dongyi
     * @date 2019-08-03 09:49
     **/
    @ApiOperation(value = "禁用启用行政区划", notes = "禁用启用行政区划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true),
            @ApiImplicitParam(name = "enableFlag", value = "禁用启用标志", required = true)
    })
    @GetMapping(value = "/updateEnableFlag/{id}/{enableFlag}")
    public JsonResult updateEnableFlag(@NotEmpty(message = "ID不能为空") @PathVariable String id,
                                       @NotEmpty(message = "禁用启用标志不能为空") @PathVariable String enableFlag) {
        return baseAreaService.updateEnableFlag(id, enableFlag);
    }

    /**
     * @description 根据编码或名称分页查询行政区划
     * @author dongyi
     * @date 2019-08-03 09:50
     **/
    @ApiOperation(value = "根据父ID或编码或名称分页查询行政区划", notes = "根据编码或名称分页查询行政区划")
    @PostMapping(value = "/areaPageSearch")
    public JsonResult<CommPager<BaseAreaDto>> areaPageSearch(@Validated @RequestBody BaseAreaQueryPageRequest pageRequest) {
        CommPager<BaseAreaDto> commPager = baseAreaService.areaPageSearch(pageRequest);
        JsonResult<CommPager<BaseAreaDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(commPager);
        return jsonResult;
    }

    /**
     * @description 根据行政区划查找组织
     * @author dongyi
     * @date 2019-08-03 09:50
     **/
    @ApiOperation(value = "根据行政区划查找组织", notes = "根据行政区划查找组织")
    @PostMapping(value = "/orgPage")
    public JsonResult<CommPager<SysOrganizationDto>> orgPage(@Valid @RequestBody BaseAreaOrgPageRequest pageRequest) {
        CommPager<SysOrganizationDto> commPager = sysOrganizationAreaService.selectOrgByArea(pageRequest);
        JsonResult<CommPager<SysOrganizationDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(commPager);
        return jsonResult;
    }

    /**
     * @description 根据行政区划查找员工
     * @author dongyi
     * @date 2019-08-03 09:50
     **/
    @ApiOperation(value = "根据行政区划查找员工", notes = "根据行政区划查找员工")
    @PostMapping(value = "/employeePage")
    public JsonResult<CommPager<BaseEmployeePositionDto>> employeePage(@Valid @RequestBody BaseAreaEmployeePageRequest pageRequest) {
        CommPager<BaseEmployeePositionDto> commPager = baseAreaService.selectEmployeeListByArea(pageRequest);
        JsonResult<CommPager<BaseEmployeePositionDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(commPager);
        return jsonResult;
    }

    /**
     * <查询组织行政区划>
     *
     * @param pageRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.tntframe.core.common.model.CommPager < com.shinho.crm.base.api.dto.response.BaseAreaDto>>
     * @author xy
     * @date 2019/8/3 11:08
     */
    @ApiOperation(value = "查询组织行政区划", notes = "查询组织行政区划")
    @PostMapping(value = "/pageSysOrganizationArea")
    public JsonResult<CommPager<BaseAreaDto>> pageSysOrganizationArea(@Valid @RequestBody BaseOrganizationAreaPageRequest pageRequest) {
        CommPager<BaseAreaDto> commPager = baseAreaService.pageSysOrganizationArea(pageRequest);
        JsonResult<CommPager<BaseAreaDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(commPager);
        return jsonResult;
    }

    /**
     * @description 查询行政区划详情
     * @author dongyi
     * @date 2019-08-05 16:50
     **/
    @ApiOperation(value = "查询行政区划详情", notes = "查询行政区划详情")
    @ApiImplicitParam(paramType = "String", name = "id", value = "行政区划ID", required = true)
    @GetMapping(value = "get/{id}")
    public JsonResult<BaseAreaDto> get(@NotEmpty(message = "id不能为空") @PathVariable String id) {
        return baseAreaService.selectAreaDetail(id);
    }

    /**
     * @description 根据层级查询行政区划列表
     * @author dongyi
     * @date 2019-08-03 09:49
     **/
    @ApiOperation(value = "根据层级查询行政区划列表", notes = "根据层级查询行政区划列表")
    @ApiImplicitParam(paramType = "Integer", name = "level", value = "层级", required = true)
    @GetMapping(value = "/levelList/{level}")
    public JsonResult<List<BaseAreaLevelDto>> levelList(@NotEmpty(message = "层级不能为空") @PathVariable Integer level) {
        return baseAreaService.levelList(level);
    }

    @ApiOperation(value = "组织查询行政区划", notes = "组织查询行政区划")
    @ApiImplicitParam(paramType = "String", name = "organizationId", value = "组织id", required = true)
    @GetMapping(value = "/areaListByOrganizationId/{organizationId}")
    public JsonResult<List<BaseAreaLevelDto>> areaListByOrganizationId(@NotEmpty(message = "organizationId不能为空") @PathVariable String organizationId) {
        return baseAreaService.areaListByOrganizationId(organizationId);
    }

    @ApiOperation(value = "排除查询行政区划", notes = "排除查询行政区划")
    @PostMapping(value = "/excludeQueryAreaList")
    public JsonResult<List<BaseAreaDto>> excludeQueryAreaList(@Valid @RequestBody BaseAreaListRequest request) {
        return baseAreaService.excludeQueryAreaList(request);
    }


    @ApiOperation(value = "根据员工查询授权组织的所有行政区划", notes = "根据员工查询授权组织的所有行政区划")
    @PostMapping(value = "/areaListByEmployee")
    public JsonResult<List<BaseAreaByEmployeeDto>> areaListByEmployee(){
        return baseAreaService.areaListByEmployee();
    }

    /**
     * @description 查询员工组织下所有行政区划，包含所有下级
     * @author dongyi
     * @date 2019-08-03 09:50
     **/
    @ApiOperation(value = "查询员工组织下所有行政区划，包含所有下级", notes = "查询员工组织下所有行政区划，包含所有下级")
    @PostMapping(value = "/allSonAreaPage")
    public JsonResult<CommPager<BaseAreaAllSonDto>> allSonAreaPage(@Validated @RequestBody BaseAreaQueryPageRequest pageRequest) {
        return baseAreaService.getAllSonAreaByEmployee(pageRequest);
    }

    /**
     * 描述 根据区划ID获取组织权限下的区划市场
     * @author 郑样平
     * @date 2019/11/8 11:40
     * @param areaId
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List<com.shinho.crm.base.api.dto.response.area.BaseAreaDto>>
     */
    @ApiOperation(value = "根据区划ID获取组织权限下的区划市场", notes = "根据区划ID获取组织权限下的区划市场")
    @ApiImplicitParam(paramType = "String", name = "areaId", value = "行政区id", required = true)
    @GetMapping(value = "/getSonAreaByEmployee/{areaId}")
    public JsonResult<List<BaseAreaDto>> getSonAreaByEmployee(@PathVariable String areaId) {
        return baseAreaService.getSonAreaByEmployee(areaId);
    }

    /**
     * 描述 当前员工， 授权组织下的所有行政区划
     * @author 郑样平
     * @date 2019/11/8 11:41
     * @param
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List<com.shinho.crm.base.api.dto.response.area.BaseAreaDto>>
     */
    @ApiOperation(value = " 当前员工， 授权组织下的所有行政区划", notes = " 当前员工， 授权组织下的所有行政区划")
    @GetMapping(value = "/getAreaEmployeeList")
    public JsonResult<List<BaseAreaDto>> getAreaEmployeeList() {
        return baseAreaService.areaEmployeeList();
    }

    @ApiOperation(value = "同步员工查询", notes = "同步员工查询")
    @PostMapping(value = "/findListBySynchro")
    public JsonResult<List<BaseAreaDto>> findListBySynchro(@Valid @RequestBody List<String> areaNames) {
        return baseAreaService.findListBySynchro(areaNames);
    }

    @ApiOperation(value = "是否区划的权限", notes = "是否区划的权限")
    @GetMapping(value = "/allAreaFlag1")
    public JsonResult<String> allAreaFlag1() {
        return baseAreaService.allAreaFlag1();
    }

}
